package cn.edu.gench.learn.day9;

import org.springframework.stereotype.Service;

import java.sql.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/26.
 */
@Service
public class CityDao {


    public List<CityModel> getCity(){
            List<CityModel> cityModels = new ArrayList<CityModel>();

            try {
                // 加载驱动程序
                Connection conn = ConnectFactory.getConnection();
                Statement statement = conn.createStatement();
                // 要执行的SQL语句
                String querySql = "select * from d8_city";
                ResultSet rs = statement.executeQuery(querySql);
                while (rs.next()) {
                    CityModel cityModel = new CityModel();
                    cityModel.setName(rs.getString("name"));
                    cityModel.setId(rs.getInt("id"));
                    cityModel.setCreatedAt(rs.getDate("created_at"));
                    cityModels.add(cityModel);
                }
                ConnectFactory.close(conn,rs,statement);
            } catch(SQLException e) {
                e.printStackTrace();
            }
            return cityModels;
        }


    public CityModel getCityOne(int id) {
        CityModel cityModel = new CityModel();
        try {
            // 加载驱动程序
            Connection conn = ConnectFactory.getConnection();
            Statement statement = conn.createStatement();
            // 要执行的SQL语句
            String querySql = "select * from d8_city where id = " + id;
            ResultSet rs = statement.executeQuery(querySql);
            while (rs.next()) {
                cityModel.setName(rs.getString("name"));
                cityModel.setId(rs.getInt("id"));
                cityModel.setCreatedAt(rs.getDate("created_at"));
            }
            ConnectFactory.close(conn,rs,statement);
        } catch(SQLException e) {
            e.printStackTrace();
        }
        return cityModel;
    }
}
